package cn.tianya.irock.security;

import cn.tianya.irock.enums.AccessControlEntry;
import cn.tianya.irock.enums.ApplicationType;
import cn.tianya.irock.enums.Permission;

public abstract class PermissionBase {
	private int	siteSettingsId;
	private int sectionId;
	private int roleId;
	//private String roleName;
	private long allowMask;
	private long denyMask;
	private ApplicationType appType = ApplicationType.Unknown;
	
	public PermissionBase() {
		allowMask = 0;
        denyMask = 0;
	}
	public int getSectionId() {
		return sectionId;
	}
	public void setSectionId(int value) {
		this.sectionId = value;
	}
	public int getRoleId() {
		return roleId;
	}
	public void setRoleId(int roleId) {
		this.roleId = roleId;
	}
	//public String getRoleName() {
	//	return roleName;
	//}
	//public void setRoleName(String roleName) {
	//	this.roleName = roleName;
	//}
	public long getAllowMask() {
		return allowMask;
	}
	public void setAllowMask(long allowMask) {
		this.allowMask = allowMask;
	}
	public long getDenyMask() {
		return denyMask;
	}
	public void setDenyMask(long denyMask) {
		this.denyMask = denyMask;
	}
	public ApplicationType getApplicationType() {
		return appType;
	}
	public void setApplicationType(ApplicationType appType) {
		this.appType = appType;
	}
	
	public boolean getBit(long p) {
		boolean bRet = false;
		
		if ((denyMask & p) == p) 
			bRet = false;
		
		if ((allowMask & p) == p) 
			bRet = true;
		return bRet;
	}
	
	public void setBit(long p, AccessControlEntry ac) {
		switch(ac.getValue()) {
		case 1:// Allow
			allowMask = allowMask | (p & -1);
			denyMask = denyMask & ~(p & -1);
			break;
		case 2: // NotSet
			allowMask = allowMask & ~(p & -1);
			denyMask = denyMask & ~(p & -1);
			break;
		default:// Deny
			allowMask = allowMask & ~(p & -1);
			denyMask = denyMask | ~(p & -1);
			break;
		}
	}
	
	public void merge(PermissionBase pb) {
		this.allowMask	|= pb.allowMask;
        this.denyMask	|= pb.denyMask;
	}
	
	public boolean view() {
		return getBit(Permission.View);
	}
	public boolean post() {
		return getBit(Permission.Post);
	}
	public boolean edit() {
		return getBit(Permission.Edit);
	}
	public boolean delete() {
		return getBit(Permission.Delete);
	}
	public boolean vote() {
		return getBit(Permission.Vote);
	}
	public boolean createPoll() {
		return getBit(Permission.CreatePoll);
	}
	public int getSiteSettingsId() {
		return siteSettingsId;
	}
	public void setSiteSettingsId(int value) {
		this.siteSettingsId = value;
	}
}
